<template>
  <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="600px" :close-on-click-modal="true" @close="close">
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="原密码" prop="oldPassWord">
        <el-input v-model="form.oldPassWord" type="password" autocomplete="off" placeholder="请输入原密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="passWord">
        <el-input v-model="form.passWord" type="password" autocomplete="off" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码">
        <el-input v-model="qCPassWord" type="password" autocomplete="off" placeholder="请确认新密码"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="medium" @click="dialogFormVisible = false">取 消</el-button>
      <el-button size="medium" type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { modifyPossword } from '@/api/shop';
import { mapState } from 'vuex';
export default {
  data() {
    return {
      rules: {
        oldPassWord: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
        passWord: [{ required: true, message: '请输入新密码', trigger: 'blur' }]
      },
      dialogFormVisible: false,
      form: {
        id: '',
        oldPassWord: '',
        passWord: ''
      },
      qCPassWord: ''
    };
  },
  computed: {
    ...mapState(['storeId'])
  },
  methods: {
    init() {
      this.form.id = this.storeId;
      this.dialogFormVisible = true;
    },
    close() {
      this.$emit('close');
    },
    save() {
      if (this.form.passWord == this.qCPassWord) {
        this.$refs.form.validate((flag) => {
          if (flag) {
            modifyPossword(this.form).then((res) => {
              if (res.code == 1) {
                this.$baseMessage('修改成功！', 'success');
                this.dialogFormVisible = false;
              }
            });
          }
        });
      } else {
        this.$baseMessage('两次密码不一致！', 'warning');
      }
    }
  }
};
</script>